package cn.tedu.mall.book.service.impl;

import cn.tedu.mall.book.entity.Consignee;
import cn.tedu.mall.book.ex.ServiceException;
import cn.tedu.mall.book.mapper.ConsigneeMapper;
import cn.tedu.mall.book.pojo.dto.ConsigneeDTO;
import cn.tedu.mall.book.pojo.vo.ConsigneeVO;
import cn.tedu.mall.book.service.IConsigneeService;
import cn.tedu.mall.book.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zxjun
 * @date 2022/6/30 8:51
 */
@Service
@Slf4j
public class ConsigneeServiceImpl implements IConsigneeService {
    @Autowired
    private ConsigneeMapper consigneeMapper;


    @Override
    public void addNew(ConsigneeDTO consigneeDTO) {
        Consignee consignee = new Consignee();
        BeanUtils.copyProperties(consigneeDTO,consignee);
        consignee.setGmtCreate(LocalDateTime.now());
        consignee.setGmtModified(LocalDateTime.now());
        int row = consigneeMapper.insert(consignee);
        if (row!=1){
            throw new ServiceException(State.ERR_INSERT,"新增收货人失败,服务器忙,请稍后重试");
        }
    }

    @Override
    public void updateById(ConsigneeDTO consigneeDTO) {
        ConsigneeVO consignee = consigneeMapper.getById(consigneeDTO.getId());
        if (consignee==null){
            throw new ServiceException(State.ERR_NOT_FOUND,"您所要修改的收货人信息不存在");
        }
        int row = consigneeMapper.updateById(consigneeDTO);
        if (row!=1){
            throw new ServiceException(State.ERR_UPDATE,"修改收货人失败,服务器忙,请稍后重试");
        }
    }

    @Override
    public void updateIsDisplayById(Integer id) {
        ConsigneeVO consignee = consigneeMapper.getById(id);
        if (consignee==null){
            throw new ServiceException(State.ERR_NOT_FOUND,"您所要修改的收货人信息不存在");
        }
        int row = consigneeMapper.updateIsDisplayById(id);
        if (row!=1){
            throw new ServiceException(State.ERR_UPDATE,"修改收货人信息失败,服务器忙,请稍后重试");
        }
        List<ConsigneeVO> list = consigneeMapper.getList();
        for (ConsigneeVO consigneeVO : list) {
            if (!consigneeVO.getId().equals(id)){
                consigneeMapper.updateIsNotDisplayById(consigneeVO.getId());
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        ConsigneeVO consignee = consigneeMapper.getById(id);
        if (consignee==null){
            throw new ServiceException(State.ERR_NOT_FOUND,"您所要删除的收货人信息不存在");
        }
        int row = consigneeMapper.deleteById(id);
        if (row!=1){
            throw new ServiceException(State.ERR_DELETE,"删除收货人失败,服务器忙,请稍后重试");
        }
    }

    @Override
    public ConsigneeVO getById(Integer id) {
        ConsigneeVO consigneeVO = consigneeMapper.getById(id);
        if (consigneeVO==null){
            throw new ServiceException(State.ERR_BAD_REQUEST,"参数异常,所要查询的收货人不存在");
        }
        return consigneeVO;
    }

    @Override
    public ConsigneeVO getDisplay() {
        ConsigneeVO consigneeVO = consigneeMapper.getDisplay();
        return consigneeVO;
    }

    @Override
    public List<ConsigneeVO> getList() {
        return consigneeMapper.getList();
    }
}
